package com.xhm.bbs.service;

import com.xhm.bbs.dao.BbsDAO;
import com.xhm.bbs.dao.UserDAO;
import com.xhm.bbs.pojo.dto.UserAddDTO;
import com.xhm.bbs.pojo.dto.UserUpdateDTO;
import com.xhm.bbs.pojo.mapper.UserMapper;
import com.xhm.bbs.pojo.po.UserPO;
import com.xhm.bbs.pojo.qo.UserQO;
import com.xhm.bbs.pojo.vo.UserListVO;
import com.xhm.bbs.pojo.vo.UserShowVO;
import com.xhm.common.constant.ErrorCode;
import com.xhm.common.exception.BusinessException;
import com.xhm.common.optimistic.OptimisticLock;
import com.xhm.common.pojo.qo.OptionQO;
import com.xhm.common.pojo.vo.OptionVO;
import com.xhm.common.pojo.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【用户】增删改查服务
 *
 * @author mrj
 * @date 2020/01/01
 */
@Service
public class UserService {

    @Autowired
    private BbsDAO bbsDAO;
    @Autowired
    private UserDAO userDAO;


    /**
     * 新增【用户】
     *
     * @param userDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public UserPO save(UserAddDTO userDTO) {
        UserPO user = UserMapper.INSTANCE.fromAddDTO(userDTO);
        userDAO.save(user);
        return user;
    }

    /**
     * 修改【用户】
     *
     * @param userUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @OptimisticLock
    public UserPO update(UserUpdateDTO userUpdateDTO) {
        Integer id = userUpdateDTO.getId();
        UserPO user = this.getUser(id, true);
        UserMapper.INSTANCE.setUpdateDTO(user, userUpdateDTO);
        userDAO.update(user);
        return user;
    }

    /**
     * 查询分页列表
     *
     * @param userQO
     * @return
     */
    public PageVO<UserListVO> list(UserQO userQO) {
        PageVO<UserListVO> page = userDAO.findByPage(userQO);
        return page;
    }

    /**
     * 查询【用户】选项列表
     *
     * @return
     */
    public List<OptionVO<Integer, String>> findOptions(OptionQO<Integer, String> qo) {
        List<OptionVO<Integer, String>> options = userDAO.findOptions(qo);
        return options;
    }

    /**
     * 根据主键获取【用户】
     *
     * @param id 主键
     * @param force 是否强制获取
     * @return
     */
    public UserPO getUser(Integer id, boolean force){
        UserPO user = userDAO.findById(id);
        if (force && user == null) {
            throw new BusinessException(ErrorCode.RECORD_NOT_FIND);
        }
        return user;
    }


    /**
     * 查询【用户】详情
     *
     * @param id
     * @return
     */
    public UserShowVO show(Integer id) {
        UserPO user = this.getUser(id, true);
        UserShowVO showVO = UserMapper.INSTANCE.toShowVO(user);
        return showVO;
    }

    /**
     * 删除【用户】
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(Integer... ids) {
        int count = 0;
        for (Integer id : ids) {
            this.checkDeleteByBbs(id);
            count += userDAO.delete(id);
        }
        return count;
    }

    /**
     * 校验是否存在【帖子】关联
     *
     * @param id
     */
    private void checkDeleteByBbs(Integer id) {
        int count = bbsDAO.getCountByUserId(id);
        if(count>0){
            throw new BusinessException(ErrorCode.CASCADE_DELETE_ERROR);
        }
    }


}


